<?php
class User_model extends CI_Model
{
    public $tab_user = 'user_user';
    private $table  = "user";
    private $table_member      = "user_detail";
    private $table_log         = "admin_login_log";
    private $table_user_online = 'user_online';
    private $table_log_login   = 'admin_login_log';
    function getRules()
    {
        return array(
            array(
                'field'=>'user_name',//ten truong trong data
                'label'=>'user name',//nhan vo van
                'rules'=>'trim|required'//bat buoc
            ),  
            array(
                'field'=>'password',//ten truong trong data
                'label'=>'password',//nhan vo van
                'rules'=>'trim|required'//
            ),  
        );
    }
    function getUserInfo($userID) {
        $user = array ();
        $user_detail = array ();

        $sql1 = "SELECT * FROM {$this->table} WHERE id = {$userID} limit 1";
        $result1 = $this->db->query ( $sql1 )->result_array ();
        if ($result1)
            $user = $result1 [0];
        $sql2 = 'SELECT * FROM ' . $this->table_member . ' WHERE id_user = ' . $userID;
        $result2 = $this->db->query ( $sql2 )->result_array ();
        if ($result2)
            $user_detail = $result2 [0];

        return array_merge ( $user, $user_detail );
    }
    function checkLogin($user_name, $password)
    {
        $user = $this->db->where('user_name', $user_name)
            ->where('password', md5($password))
            ->join('user_role', 'user_role.role_id = user_user.role_id')
            ->get($this->tab_user)
            ->row();
        if(empty($user))
        {
            return false;
        }
        else
        {
            return $user;
        }
    }
    /**
     * @Desc Kiểm tra thông tin user có tồn tại hay không
     * @param $field truong thong tin: Username, email
     * @param $value: Gia tri nguoi dung nhap vao
     * @return True | False
     */
    function checkUserInfo($field, $value) {
        if ($field) {
            $sql = 'SELECT id FROM ' . $this->table . ' WHERE ' . $field . ' = "' . $value . '"';
            $check = $this->db->query ( $sql );
            if ($check->num_rows () >= 1)
                return false;
        }
        return true;
    }
    /**
     * @desc Insert thông tin vào CSDL khi đăng ký
     * @param $arr: Mang thong tin can inssert
     * @return ID insert
     */
    function register($arr, $detail = false) {
        if (! $detail) {
            //$check1 = $this->checkUserInfo('username', $arr['username']);
            $check2 = $this->checkUserInfo('email', $arr['email']);
            $arr ['date_register'] = time ();
            if($check2== true){
                if ($this->db->insert ( $this->table, $arr ))
                    return $this->db->insert_id ();
                else
                    return false;
            }else return false;

        } else {
            if ($this->db->insert ( $this->table_member, $arr ))
                return $this->db->insert_id ();
            else
                return false;
        }
    }
    // ham check username và pass đã tồn tại chưa
    function checkUserRegister($username, $email) {
        $sql = 'SELECT id,username, email FROM ' . $this->table . ' WHERE  username = "' . $username . '" OR email = "' . $email . '"';
        $check = $this->db->query ( $sql );
        return ($check->num_rows () > 0) ? true : false;
    }
    /**
     * @Desc Hàm  lấy thông tin User
     * @param $userID ID của user
     * @return array
     */
    function activate($uid) {
        $sql = 'UPDATE  ' . $this->table . ' SET  status =  1 WHERE  id = ' . $uid;
        return $this->db->query ( $sql );
    }

    /**
     * đang nhập tài khoản user
     */
    function login($username, $pass) {
        $sql = 'SELECT *
				FROM ' . $this->table . '
				WHERE username = "' . $username . '" AND `pass`="' . $pass . '" ';
        $item = $this->db->query ( $sql );
        if ($item->num_rows () == 1) {
            $mem = $item->result_array ();
            return $mem [0];
        }
        return NULL;
    }
    /**
     * insert dữ liệu vào khi log
     */
    function insert_log($data) {
        return $this->db->insert ( $this->table_log, $data );
    }
    /**
     * @Desc Hàm lấy thông tin user theo email
     * @param $email: email truyền vào
     * @return True|False
     */
    function getUserById($uid) {
        $sql = 'SELECT * FROM ' . $this->table . ' WHERE id = ' . $uid;
        $result = $this->db->query ( $sql )->result_array ();
        if ($result)
            return $result [0];
        else
            return false;
    }
    /**
     * @Desc Hàm update session khi thành viên offline
     * @param
     * @return array
     */
    function update_session() {
        $sql = 'UPDATE ' . $this->table_user_online . ' SET user_id = -1, username = "" WHERE session_id = "' . $this->session->userdata ( 'session_id' ) . '"';
        return $this->db->query ( $sql );
    }
}